<template>
  <a-card :bordered="false">
    <a-row :gutter="24">
      <a-form :form="form" @submit="handleSubmit">
        <a-col :md="12">
          <a-form-item v-bind="formItemLayout" label="门类名称">
            <a-select
              allowClear
              v-decorator="['mingCheng',{
                  rules: [
                    {
                      required: true,
                      message: '门类名称不能为空',
                    },
                  ],
                }]"
              @change="handleChange"
            >
              <a-select-option
                v-for="item in mlTree"
                :key="item.value"
                :value="item.value"
              >{{item.text}}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12">
          <a-form-item v-bind="formItemLayout" label="门类代码">
            <a-input
              disabled
              placeholder="请输入门类代码"
              v-decorator="['daiMa',{
                  rules: [
                    {
                      required: true,
                      message: '门类代码不能为空',
                    },
                  ],
                }]"
            />
          </a-form-item>
        </a-col>
        <a-col :md="12">
          <a-form-item v-bind="formItemLayout" label="父级标识">
            <a-tree-select
              v-decorator="['shangJi']"
              show-search
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              placeholder="请选择父级标识"
              allow-clear
              tree-default-expand-all
              @change="onChangeTree"
            >
              <a-tree-select-node
                v-for="item in treeData"
                :key="item.id"
                :value="item.id"
                :title="item.mingCheng"
              >
                <a-tree-select-node
                  v-for="items in item.childList"
                  :key="items.id"
                  :value="items.id"
                  :title="items.mingCheng"
                >
                  <a-tree-select-node
                    v-for="itemss in items.childList"
                    :key="itemss.id"
                    :value="itemss.id"
                    :title="itemss.mingCheng"
                  />
                </a-tree-select-node>
              </a-tree-select-node>
            </a-tree-select>
          </a-form-item>
        </a-col>
        <!-- <a-col :md="12">
          <a-form-item v-bind="formItemLayout" label="序号">
            <a-input-number
              style="width: 100%"
              id="inputNumber"
              placeholder="请输入序号"
              v-decorator="['ciXu',{
                  rules: [
                    {
                      required: true,
                      message: '序号不能为空',
                    },
                  ],
                }]"
              :min="1"
            />
          </a-form-item>
        </a-col>-->
        <a-col :md="12">
          <a-form-item v-bind="formItemLayout" label="使用状态">
            <a-select
              allowClear
              placeholder="请选择使用状态"
              v-decorator="['zhuangTai',{
                  rules: [
                    {
                      required: true,
                      message: '使用状态不能为空',
                    },
                  ],
                }]"
            >
              <a-select-option :value="0">未启用</a-select-option>
              <a-select-option :value="1">已启用</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="24">
          <a-form-item v-bind="formItemLayout2" label="备注">
            <a-textarea placeholder="请输入备注" :rows="4" v-decorator="['beiZhu']" />
          </a-form-item>
        </a-col>
        <a-col :md="24">
          <div class="antBtn">
            <a-button type="primary" html-type="submit">保存</a-button>
            <a-button type="primary" style="margin-left:8px" @click="fanHui">返回</a-button>
          </div>
        </a-col>
      </a-form>
    </a-row>
  </a-card>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
export default {
  name: 'MenLeiNew',
  components: {},
  data() {
    return {
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
      },
      formItemLayout2: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
      },
      form: this.$form.createForm(this, { name: 'register' }),
      treeData: [],

      // 门类名称
      mlTree: [],
      fj: this.$route.query.fj,
      isO: false,
    }
  },
  created() {
    this.form.resetFields()
    // 门类树
    getAction('/xtMenLei/tree', {}).then((res) => {
      if (res.code == 200) {
        this.treeData = res.result
      }
    })
    // 门类名称
    getAction('/sys/dict/getDictItems/MenLeiMingCheng', {}).then((res) => {
      if (res.code == 0) {
        this.mlTree = res.result
      }
    })
    const {
      form: { setFieldsValue },
    } = this
    this.$nextTick(() => {
      setFieldsValue({
        shangJi: this.fj,
      })
    })
  },
  methods: {
    // 提交
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          if (this.isO) {
            values.shangJi = values.shangJi
          } else {
            values.shangJi = values.shangJi.join()
          }
          let body = values
          postAction('/xtMenLei/add', { ...body }).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.message)
              this.$router.push({ path: '/menlei' })
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    // 返回
    fanHui() {
      this.$router.push({ path: '/menlei' })
    },
    // 门类树选择
    onChangeTree(value) {
      this.isO = true
    },
    // 门类选择
    handleChange(value) {
      const {
        form: { setFieldsValue },
      } = this
      this.$nextTick(() => {
        setFieldsValue({
          daiMa: value,
        })
      })
    },
  },
}
</script>

<style lang="less">
.antBtn {
  text-align: right;
  margin: 30px auto 0;
}
.transfer {
  text-align: center;
}
.transfer .ant-transfer-list {
  text-align: left;
}
.transfer .ant-transfer-operation {
  margin: 0 48px;
}
.transfer-Se {
  text-align: center;
}
.transfer-Se .ant-select {
  position: relative;
  top: -50px;
}
.antAdd {
  font-size: 24px;
  cursor: pointer;
}
.antAdd span:nth-child(1) {
}
.antAdd span:nth-child(2) {
  margin-left: 30px;
}
</style>